<?php
/**
 * create table table2 like table1;
 * insert into table2 select * from table1 WHERE your_where_clause;
 * delete from table1 WHERE your_where_clause;
 * (optional) OPTIMIZE TABLE table1;
 *
 * link http://www.europeanexperts.org/question/1519.html&lazone=MySql&c=844
 * see module setup.php
 */
global $m, $tab, $a;
require_once($AppUI->getModuleClass('projects'));
require_once($AppUI->getMasterDataClass('mdtype'));
require_once($AppUI->getMasterDataClass('mdstatus'));

$df = $AppUI->getPref('SHDATEFORMAT');

$perm = &$AppUI->acl();
if (!$perm->userHasRole($AppUI->user_id, 'Accounting')) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect("m=home&tab=0");
	exit;
}

$beingArchive = dPgetParam($_POST, 'beingArchive', 0);
if ($beingArchive) {
	include(DP_BASE_DIR."/modules/project_archived/do_many_projects_archive.php");
	return;
}

if (isset($_POST['resetAction'])) {
	$AppUI->unsetPOST('ARCHIVE_POST');
}
else {
	if (count($_POST) > 0) {
		$AppUI->savePOST('ARCHIVE_POST');
	}
	else {
		$_POST = $AppUI->getPOST('ARCHIVE_POST');
	}
}
$AppUI->savePlace();

$page_offset = intval( dPgetParam( $_GET, 'page_offset', 0 ) );
$page_limit = intval( dPgetParam( $_POST, 'page_limit', 10));

//get post params
$customer = intval( dPgetParam( $_POST, 'project_company', 0 ) );
$keyword = dPgetParam( $_POST, 'keyword', '' );
$from_date = dPgetParam( $_POST, 'complete_from_date', null );
if ($from_date != '') {
	$from_date = new CDate($from_date);
}
$to_date = dPgetParam( $_POST, 'complete_to_date', null );
if ($to_date != '') {
	$to_date = new CDate($to_date);
}

$canViewSalesAmount = $perm->checkModuleItem("taskfee", "view");
$canEditSalesAmount = $perm->checkModuleItem("taskfee", "edit");
$canDelete = $perm->checkModuleItem("projects", "delete");

// Load Object Status
$process_status = new CMDStatus();
$project_processed = dPgetSysVal( 'ProjectProcessed' );
$process_status->loadByCode($project_processed[0]);
$_POST['project_rstatus'] = $process_status->status_id;

$pro = new CProject();
$totalRows = 0;
$projects = $pro->getList($_POST, $totalRows);

require_once($AppUI->getMasterDataClass('mdcurrency'));
$objMD = new CMdCurrency();
$curArr = $objMD->getCurrencyList();

$objMD = new CCompany();
$cusList = $objMD->getAllowedRecords($AppUI->user_id, 'company_id, company_name', 'company_name');
$cusList = arrayMerge(array('0'=>"" . $AppUI->_(''). ""), $cusList);
?>
<form name="reportFrm" action="?m=<?=$m?>&tab=<?=$tab?>" method="post">
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="infopanel">
<tr><td width="50%">
<table cellspacing="0" cellpadding="4" border="0" width="100%" >
<tr>
	<td align="right" width="100px"><?=$AppUI->_('From date')?></td>
	<td nowrap="nowrap" width="100px">
		<input id="idDateFrom" type="hidden" name="complete_from_date"
				value="<?=$from_date ? $from_date->format( FMT_TIMESTAMP_DATE ) : ''?>" />
		<input id="idDateFromD" type="text" style="width: 80px"
				value="<?=$from_date ? $from_date->format( $df ) : ''?>"
				class="text" disabled="disabled" />
		<img id="idDateFromB" src="images/calendar.gif" title="<?=$AppUI->_('Calendar')?>" border="0">
	</td>
	<td align="right" width="80px"> <?=$AppUI->_('To date')?></td>
	<td nowrap="nowrap">
		<input id="idDateTo" type="hidden" name="complete_to_date"
				value="<?=$to_date ? $to_date->format( FMT_TIMESTAMP_DATE ) : ''?>" />
		<input id="idDateToD" type="text" style="width: 80px"
				value="<?=$to_date ? $to_date->format( $df ) : ''?>"
				class="text" disabled="disabled" />
		<img id="idDateToB" src="images/calendar.gif" title="<?=$AppUI->_('Calendar')?>" border="0">
	</td>
</tr>
<tr>
	<td align="right"><?=$AppUI->_('Reference code')?></td>
	<td colspan="100%"><input type="text" name="keyword" value="<?=@$keyword?>" style="width: 316px"/></td>
</tr>
<tr>
	<td align="right"><?=$AppUI->_('Customer')?></td>
	<td colspan="100%"><?= arraySelect($cusList, 'project_company', ' id="customer" size="1" class="text" style="width: 320px;"', $customer, true)?></td>
</tr>
</table>
</td>
<td>
</tr></td>
</table>
<table cellspacing="0" cellpadding="6" border="0" width="100%" class="subinfopanel">
<tr>
	<td>
		<input class="button" type="submit" name="searchAction" value="<?=$AppUI->_('Search');?>"/>
		<input class="button" type="submit" name="resetAction" value="<?=$AppUI->_('All');?>"/>
	</td>
</tr>
</table>
</form>
<form name="archiveFrm" id="archiveFrm" action="?m=<?=$m?>&tab=<?=$tab?>" method="POST">
<input type="hidden" name="beingArchive" value="0"/>
<table id="idReferenceList" class="list" cellspacing="0" cellpadding="2" border="0" width="100%">
		<tr class="<?=($i % 2 == 0) ? "second" : "first" ?>">
			<th width="30px" rowspan="2" align="center">
				<input type="checkbox" id="checkAllArchive" name="checkAllArchive" value="1" onclick="checkAllCbArchive(this.form, this.checked)"/>
			</th>
			<th rowspan="2"><?=$AppUI->_('Customer code')?></th>
			<th rowspan="2"><?=$AppUI->_('Customer name')?></th>
			<th rowspan="2"><?=$AppUI->_('Reference code')?></th>
			<th rowspan="2"><?=$AppUI->_('Completed date')?></th>
			<th colspan="4" align="center"><?=$AppUI->_('Original Currency')?></th>
			<th rowspan="2"><?=$AppUI->_('Remarks')?></th>
		</tr>
		<tr>
			<th><?=$AppUI->_('Currency')?></th>
			<th><?=$AppUI->_('Sales amount')?></td>
			<th><?=$AppUI->_('VAT amount')?></th>
			<th><?=$AppUI->_('Account Receivable')?></th>
	    </tr>
<?php if (is_array($projects) && count($projects) > 0):?>

	<script type="text/javascript">
		function submitForm(offset) {
			document.reportFrm.offset.value = offset;
			document.reportFrm.submit();
		}
	</script>
	<?php
		$i=0;
		$sumSA = 0;
		$sumVAT = 0;
		$sumAR = 0;
		foreach ($projects as $row) {
			$i++;
			$currency = $row['project_currency'];
			$exchange = dPgetParam( $_POST, $currency . "_vnd", 1 );
			$sa = $row['sa_sales_amount'];
			$vat = $pro->getVATAmount($row['project_id']);
			$ar = $sa + $vat;

			$sumSA += $exchange * $sa;
			$sumVAT += $exchange * $vat;
			$sumAR += $exchange * $ar;

			$completedDate = $row['project_actual_end_date'] != '' ? new CDate($row['project_actual_end_date']) : NULL;
	?>
		<tr class="<?=($i % 2 != 0) ? "second" : "first" ?>" >
			<td align="center">
				<input type="checkbox" name="checkArchive[]" value="<?=$row['project_id']?>"/>
			</td>
			<td onClick="showReference(<?=$row['project_id']?>)" ><?=$row['company_code']?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" ><?=$row['company_name']?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" ><?=$row['project_iso_code']?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" align="center"><?=isset($completedDate) ? $completedDate->format($df) : ""?></td>

			<!--------Original currency---------------->
			<td onClick="showReference(<?=$row['project_id']?>)" align="center"><?=$curArr[$row['project_currency']]?></td>
			<td align='right'><?=currencyFormat($row['sa_sales_amount'], $row['project_currency'])?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?=currencyFormat($pro->getVATAmount($row['project_id']), $row['project_currency'])?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?=number_format($pro->getAccountReceivable($row['project_id']))?></td>

			<!--------Remarks---------------->
			<td onClick="showReference(<?=$row['project_id']?>)"><?=$row['project_description']?></td>
		</tr>
	<?php }?>
		<tr>
			<th colspan="100%" ><input type="button" value="<?=$AppUI->_('Complete')?>" name="archiveReference" onClick="doArchive(this.form)"/></th>
		</tr>
	</table>
<?php else:
	showEmptyRow('1', 'second', 'Not found any references', '100%');
endif;?>
</table>
</form>
<script>
	Calendar.enabled = true;

	Table("idReferenceList").setNormalHover(1);

	function showReference(id){
		location.href = "index.php?m=projects&a=view&project_id=" + id;
	}

	function checkAllCbArchive(frm, checked)
	{
		var elms = frm.elements['checkArchive[]'];
		if (!isArray(elms)) {
			elms.checked = checked;
			return;
		}
		for(i = 0; i < elms.length; i++) {
			elms[i].checked = checked;
		}
	}

	function doArchive(frm) {
		var elms = frm.elements['checkArchive[]'];
		var countChecked = 0;
		if (!isArray(elms)) {
			countChecked = elms.checked ? 1 : 0;
		} else {
			for(i = 0; i < elms.length; i++) {
				if (elms[i].checked) {
					countChecked++;
				}
			}
		}
		if (countChecked == 0) {
			alert("You should select one or more references to archive!");
			return;
		}
		if (confirm("Do you want to archive these references?")) {
			frm.elements['beingArchive'].value = 1;
			frm.submit();
		}
	}
</script>